<?php

$page_title = "Management - fifaonline.ro";
include_once("top.php");
?>
<?php if (!isset($_SESSION['identitate'])){
	header("Location: http://localhost");
	exit();
}?>


	<center>
	<?php
	//Verifica daca formularu a fost transmis.
	if(isset($_POST['submitted']))
	{
		$query = "SELECT post_id FROM posts ORDER BY post_id DESC LIMIT 1";
		$result = @mysql_query($query);
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
		{
			$number = $row['post_id'];
		}
		$number = $number+1;
		$target_path = "uploads/";
		$target_path = $target_path . $number. ".jpg";
		$errors = array(); //Initializeaza tabloul de erori.
	
		//Verifica titlul.
		if (empty($_POST["title"]))
		{
			$errors[] = 'Ai uitat sa scrii titlul.';
		}
		else
		{
			$title = trim($_POST["title"]);
		}
	
		//Verifica continutul.
		if(empty($_POST["content"]))
		{
			$errors[] = 'Ai uitat sa scrii continutul.';
		}
		else
		{
			$content = trim($_POST["content"]);
		}
		
		//Verifica sursa stirii.
		if(empty($_POST["sursa_s"]))
		{
			$errors[] = 'Ai uitat sa scrii sursa stirii.';
		}
		else
		{
			$sursa_s = trim($_POST["sursa_s"]);
		}
		
		//Verifica categoria.
		if(empty($_POST["category"]))
		{
			$errors[] = 'Pune categoria.';
		}
		else
		{
			$category = trim($_POST["category"]);
		}
		//Verifica poza
		if(move_uploaded_file($_FILES['upload']['tmp_name'], $target_path)) 
		{
   			echo "The file ".  basename( $_FILES['upload']['name']).  " has been uploaded";
		} 
		else
		{
    		echo "There was an error uploading the file, please try again!";
		}
		
		if(empty($errors)) //Daca totul e in regula
		{
			//Posteaza stirea pe homepage
			require_once ("/includes/mysql_connect.php"); //Se conecteaza la baza de date.
			
			//Creeaza interogarea
			$query = "INSERT INTO posts (post_title, post_content, post_category, news_source, date_written) VALUES ('$title', '$content', '$category', '$sursa_s', NOW() )";
			$result = @mysql_query($query); //Executa interogarea.
			if ($result) //Daca interogarea a fost realizata cu succes.
			{
				//Afiseaza un mesaj
				echo "<h1>Stire adaugata!</h1>";
				echo "<p>Stirea a fost adaugata.</p>";
			}
			else //Daca nu a mers bine.
			{
				echo "<h1>System Error!</h1>";
				echo "<p>A aparut o eroare de sistem:" . mysql_error() . "<br>Query: " . $query . "<p>";
				exit();
			}
			mysql_close(); //Inchide conexiunea cu baza de date.
		}
		else //Raporteaza erorile
		{
			echo "<h1>Erori</h1>
			<p>Au aparut urmatoarele erori:<br>";
			foreach ($errors as $msg) //Afiseaza fiecare erorare
			{
				echo "-$msg<br>\n";
			}
			echo "<br>Please try again!";
		}//Sfarsitul blocului IF(empty($errors)).
	}//Sfarsitul conditionalei principale.

?>
<form method="post" action="manage.php" enctype="multipart/form-data">
<table>
<tr>
<td><p class="text1">Titlu: </p></td>
<td> <input type="text" name="title" value=""/></td>
</tr>
<tr>
<td><br /><p class="text1">Continut: </p></td>
<td> <textarea name="content" value="<?php if(isset($_POST['content'])) echo $_POST['content'];?>"></textarea></td>
</tr>
<tr>
<td><p class="text1">Categoria: </p></td>
<td><select name="category" size="1">
	<option>UCL</option>
    <option>UEL</option>
    <option>Fotbal Romanesc</option>
    <option>Fotbal European</option>
    <option>Fotbal in afara Romaniei</option>
    </select>
    </td>
    </tr>
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<tr>
<td><br /><p class="text1">Imagine: </p></td>
<td><input type="file" name="upload" /></td>
</tr>
<tr>
<td><br /><p class="text1">Sursa stirii: </p></td>
<td><input type="text" name="sursa_s" maxlength="100" /></td>
</tr>
</table>
<input type="submit" name="submitted" value="post" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
<a href="logout.php">Logout</a><br>
<a href="quotes.php">Quotes</a><br>
<a href="poll_create.php">Polls</a>
</center>
<?php
include_once("end.php");
?>